package com.exrobot.module.rbt.controller.app;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.exrobot.framework.common.pojo.CommonResult;
import com.exrobot.framework.common.util.date.LocalDateTimeUtils;
import com.exrobot.module.rbt.controller.admin.order.vo.OrderPageReqVO;
import com.exrobot.module.rbt.controller.admin.orderitem.vo.OrderItemPageReqVO;
import com.exrobot.module.rbt.controller.app.vo.MqCommonVO;
import com.exrobot.module.rbt.dal.dataobject.canteenmenu.CanteenMenuDO;
import com.exrobot.module.rbt.dal.dataobject.order.OrderDO;
import com.exrobot.module.rbt.dal.dataobject.orderitem.OrderItemDO;
import com.exrobot.module.rbt.enums.RbtOrderStatusEnum;
import com.exrobot.module.rbt.service.canteenmenu.CanteenMenuService;
import com.exrobot.module.rbt.service.order.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.exrobot.framework.common.pojo.CommonResult.error;
import static com.exrobot.framework.common.pojo.CommonResult.success;
import static com.exrobot.module.rbt.enums.ErrorCodeConstants.RBT_ORDER_SAVECODE_FAIL;

@Tag(name = "APP - 订单接口")
@RestController
@RequestMapping("/robot/order")
@Validated
public class AppOrderController {


    @Resource
    private OrderService orderService;

    @Resource
    private CanteenMenuService menuService;

    @GetMapping("getInfoByCode")
    @Operation(summary = "获取订单信息")
    @PermitAll
    public CommonResult<JSONObject> getInfoByCode(@RequestParam("code")String code) {
        LocalDateTime[] todayRange = LocalDateTimeUtils.getTodayRange();
        LocalDateTime startTime = todayRange[0].minusDays(1);
        LocalDateTime endTime = todayRange[1];
        OrderPageReqVO reqVO = new OrderPageReqVO();
        reqVO.setSaveCode(code);
        reqVO.setOrderTime(new LocalDateTime[]{startTime,endTime});
        List<OrderDO> list = orderService.getOrderList(reqVO);
        if (list == null || list.size() != 1) {
            return error(RBT_ORDER_SAVECODE_FAIL,code);
        }
        OrderDO orderDO = list.get(0);
        OrderItemPageReqVO orderItemPageReqVO = new OrderItemPageReqVO();
        orderItemPageReqVO.setOrderSn(orderDO.getOrderSn());
        orderItemPageReqVO.setStatus(RbtOrderStatusEnum.TAKE.getVal());
        List<OrderItemDO> orderItemDOList = orderService.getOrderItemList(orderItemPageReqVO);
        if (orderItemDOList == null || orderItemDOList.isEmpty()) {
            return error(RBT_ORDER_SAVECODE_FAIL,code);
        }
        Map<Long,Long> maps = orderItemDOList.stream().collect(Collectors.groupingBy(OrderItemDO::getMenuSn,Collectors.counting()));
        List<JSONObject> array = new ArrayList<>();
        Map<Long, CanteenMenuDO> menuDOMap = menuService.getCanteenMenuMap(maps.keySet());
        for (Map.Entry<Long, Long> item : maps.entrySet()) {
            JSONObject object = new JSONObject();
            object.put("foodId", item.getKey());
            object.put("foodSize",menuDOMap.containsKey(item.getKey()) ? menuDOMap.get(item.getKey()).getFoodSize() : 1);
            object.put("num",item.getValue());
            array.add(object);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orderSn",orderDO.getOrderSn());
        jsonObject.put("canteenId",orderDO.getCanteenSn());
        jsonObject.put("items",array);
        return success(jsonObject);
    }

}